'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async list() {
    let page=parseInt(this.ctx.request.body.page);
    let limit=parseInt(this.ctx.request.body.limit);
    let offset=(page-1)*limit;
    let res=await this.app.model.User.findAll({
     offset,
     limit   
    }) 
    let count=await this.app.model.User.count()
    let obj={
        data:res,
        count:count
    }
    this.ctx.apiSuccess(obj,"获取成功",200)
  }
  async adduser(){
    let { username, password } = this.ctx.request.body;
    this.ctx.validate({
      username: {
        type: "string",
        required: true,
        defValue: "",
        desc: "用户名称",
      },
      password: { type: "string", required: true, desc: "密码" },
      name: {
        type: "string",
        required: false,
        defValue: "",
        desc: "姓名",

      },
   
   
      avatar: {
        type: "string",
        required: false,
        defValue: "",
        desc: "头像",

      }
    })
    if (await this.app.model.User.findOne({
      where: {
        username
      }
    })) {
      this.ctx.throw("该用户已存在", "该用户已存在", 400)
    }
    let res=await this.app.model.User.create({
      username,password,
   
      name:this.ctx.request.body.name,
      avatar:this.ctx.request.body.avatar,
      coin:0
    })
    if(!res){
      this.ctx.throw(res,"创建失败",500)
    }
    this.ctx.apiSuccess(res,"创建成功",200)
  }
  async edituser(){
    let { username, password } = this.ctx.request.body;
    this.ctx.validate({
      id:{  type: "int",
        required: true,
        defValue: "",
        desc: "id",
        
      },
      username: {
        type: "string",
        required: true,
        defValue: "",
        desc: "用户名称",
      },
      password: { type: "string", required: true, desc: "密码" },
      name: {
        type: "string",
        required: true,
        defValue: "",
        desc: "姓名",

      },
     coin:{
        type: "int",
        required: true,
        defValue: "",
        desc: "金币",
     },
      avatar: {
        type: "string",
        required: true,
        defValue: "",
        desc: "头像",

      }
    })
    let res=await this.app.model.User.findOne({
      where:{
        id:this.ctx.request.body.id
      }
    })
    if(!res){
      this.ctx.throw(res,"该用户不存在",500)
    }
    res.username=this.ctx.request.body.username;
    res.password=this.ctx.request.body.password;
res.coin=this.ctx.request.body.coin
    res.name=this.ctx.request.body.name;
    res.avatar=this.ctx.request.body.avatar;
    res.save()
this.ctx.apiSuccess(res,"修改成功",200)
  }
  async deleteUser(){
    this.ctx.validate({
      id:{  type: "int",
        required: false,
        defValue: "",
        desc: "id",
        
      },
    })
    let res=this.app.model.User.destroy({
      where:{
        id:this.ctx.request.body.id
      }
    })
    this.ctx.apiSuccess(res,"删除成功",200)
  }
  async updateuser(){
  
    const currentid=this.ctx.authUser.id;
    this.ctx.validate({
      type: {
        type: "string", required: true, desc: "类型"
    },
  
        name: {
            type: "string", required: false, desc: "昵称"
        },
        avatar: {
            type: "string", required: false, desc: "头像"
        },
        
    });
    let res=await this.ctx.model.User.findOne({
        where:{
            id:currentid
        }
    })

    if(!res){
      console.log(this.ctx.authUser,"authUser")
      this.ctx.apiFail("该用户不存在")
    }
    res.name=this.ctx.request.body.name
    res.avatar=this.ctx.request.body.avatar
    res.save();
    this.ctx.apiSuccess(res,"修改成功",200)

}
}

module.exports = UserController;
